<script setup lang="ts">
/**
 * 系统管理-系统数据-国民行业-编辑
 */
import {ref} from "vue";
import {ElMessage, type FormInstance} from "element-plus";
import {editStandardIndustryApi} from "@/assets/api/systemManagement/data/standardIndustry";
import type {StandardIndustry} from "@/components/interface/data/StandardIndustry";

const props = defineProps<{
  form: StandardIndustry
}>()

const emits = defineEmits([
  'save', 'close'
])

const formRef = ref()
const rules = ref({
  name: [
    {required: true, message: '请输入名称', trigger: 'blur'},
  ],
  code: [
    {required: true, message: '请输入编码', trigger: 'blur'},
  ],
  parentCode: [
    {required: true, message: '请输入父级行业代码', trigger: 'blur'},
  ],
})

// 保存
const saveLoading = ref(false)
const save = async (formRef: FormInstance) => {
  if (!formRef) return
  await formRef.validate((valid: boolean) => {
    if (valid) {
      saveLoading.value = true
      editStandardIndustryApi(props.form).then(res => {
        saveLoading.value = false
        if (res.data.code === 200) {
          ElMessage.success({message: res.data.data, showClose: true})
          emits('save', true)
        } else
          ElMessage.error({message: res.data.data, showClose: true})
      })
    }
  })
}

</script>

<template>
  <div class="form-div">
    <el-form v-loading="saveLoading" :model="props.form" :rules="rules" ref="formRef" label-width="120px">
      <el-form-item label="行业名称" prop="name">
        <el-input v-model="props.form.name" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="行业代码" prop="code">
        <el-input v-model="props.form.code" placeholder="请输入代码"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="props.form.status" style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                   :active-value="1" :inactive-value="0"/>
      </el-form-item>
      <el-form-item label="父级行业代码" prop="parentCode">
        <el-input v-model="props.form.parentCode" placeholder="请输入父级CODE"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="emits('close')">关闭</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
